﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace VarianceReportWinUI
{
    public partial class frmWorkItem : DockContentEx
    {
        public frmWorkItem()
        {
            InitializeComponent();
        }
        private WorkItem mWorkItem;
        private ListViewSorter.ListViewSortManager mListViewSortManager = null;

        public void View(WorkItem pWorkItem)
        {
            mWorkItem = pWorkItem.Clone();
            UpdateViewer();
        }

        public override void UpdateViewer()
        {
            txtName.Text = mWorkItem.Name;
            txtSource.Text = mWorkItem.Source;
            txtCreated.Text = mWorkItem.Created.StringValueOrEmpty();
            txtUpdated.Text = mWorkItem.Updated.StringValueOrEmpty();
            txtDescription.Text = mWorkItem.Description;
            UpdateCaption();
        }

        private void txtName_TextChanged(object sender, EventArgs e)
        {
            mWorkItem.Name = txtName.Text;
            UpdateCaption();
        }

        private void txtDescription_TextChanged(object sender, EventArgs e)
        {
            mWorkItem.Description = this.DockHandler.TabText;
        }

        private void UpdateCaption()
        {
            this.DockHandler.TabText = string.Format("{0} - {1}", mWorkItem.Name, mWorkItem.Created);
        }

        private void tabMain_TabIndexChanged(object sender, EventArgs e)
        {

        }

        private void UpdateFullReport()
        {

            mWorkItem.LoadExcelDataSet(false);

            var tTable = mWorkItem.WorkItemDataSet.Tables[0];

            mListViewSortManager = null;
            lstFullReport.Clear();
            List<Type> types = new List<Type>();


            for (int i = 0; i < tTable.Columns.Count; i++)
            {
                types.Add(typeof(ListViewSorter.ListViewTextSort));
                lstFullReport.Columns.Add(tTable.Columns[i].ColumnName);
            }

            

            foreach (DataRow row in tTable.Rows)
            {
                var array = row.ItemArray;

                var lvitem = lstFullReport.Items.Add(array[0].StringValueOrEmpty());

                for (int i = 1; i < array.Count(); i++)
                {
                    lvitem.SubItems.Add(array[i].StringValueOrEmpty());
                }
            }

            if (tTable.Rows.Count > 0)
            {
                for (int i = 0; i < lstFullReport.Columns.Count; i++)
                {
                    lstFullReport.Columns[i].AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
                }
            }


            mListViewSortManager = new ListViewSorter.ListViewSortManager(lstFullReport, types.ToArray());



        }

        private void tabMain_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabMain.SelectedTab == tabFullReport)
            {
                UpdateFullReport();
            }
        }

        private void mnuMainSave_Click(object sender, EventArgs e)
        {
            var remove = uiHandler.workQueue.WorkItemList.First(t => t.Key == mWorkItem.Key);
            var index = uiHandler.workQueue.WorkItemList.IndexOf(remove);
            uiHandler.workQueue.WorkItemList.Remove(remove);
            uiHandler.workQueue.WorkItemList.Insert(index, mWorkItem);
            mWorkItem.Updated = DateTime.Now;
            uiHandler.queuePanel.UpdateViewer();
            uiHandler.workQueue.Save();
            this.Close();

        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            var remove = uiHandler.workQueue.WorkItemList.First(t => t.Key == mWorkItem.Key);
            var index = uiHandler.workQueue.WorkItemList.IndexOf(remove);
            uiHandler.workQueue.WorkItemList.Remove(remove);
            mWorkItem.Updated = DateTime.Now;
            uiHandler.workQueue.WorkItemList.Insert(index, mWorkItem);
            uiHandler.queuePanel.UpdateViewer();
            uiHandler.workQueue.Save();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            var remove = uiHandler.workQueue.WorkItemList.First(t => t.Key == mWorkItem.Key);
            var index = uiHandler.workQueue.WorkItemList.IndexOf(remove);
            uiHandler.workQueue.WorkItemList.Remove(remove);
            uiHandler.queuePanel.UpdateViewer();
            uiHandler.workQueue.Save();
            this.Close();
        }

        private void btnExport_Click(object sender, EventArgs e)
        {

        }

    }
}
